package test.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

import test.model.Greeting;
import test.model.HelloMessage;

/**
 * Copyright © 2018 掌上医讯. All rights reserved.
 * 
 * @Title: GreetingController.java
 * @Prject: springwebsocket
 * @Package: test
 * @Description: TODO
 * @author: QuZening
 * @date: 2018年3月20日 下午4:59:58
 * @version: V1.0
 */
@Controller
public class GreetingController {
	
	@Autowired
	private SimpMessagingTemplate simpMessagingTemplate;

	@MessageMapping("/hello")
	@SendTo("/topic/greetings")
	public Greeting greeting(HelloMessage message) throws Exception {
		Thread.sleep(1000); // simulated delay
		return new Greeting("Hello, " + message.getName() + "!");
	}
	
	@MessageMapping("/hello2")
	public void singleGreeting(HelloMessage message) {
		simpMessagingTemplate.convertAndSendToUser("admin", "/topic/greetings", new Greeting(message.getName()));
	}

}